package com.sinodata.bsm.cicp.finder.vsphere;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;

import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.cicp.util.SnmpUtil;

public class EsxOS extends Finder {
    private static Logger logger = Logger.getLogger(EsxOS.class);
    // 主机描述
    private static final String SYS_DESCRIPT_OID = ".1.3.6.1.2.1.1.1.0";
    // 主机名称
    private static final String SYS_NAME_OID = ".1.3.6.1.2.1.1.5.0";
    // 物理cpu个数
    private static final String PHY_CPU_NUM_OID = ".1.3.6.1.4.1.6876.3.1.1.0";
    // 物理内存大小
    private static final String PHY_MEM_SIZE_OID = ".1.3.6.1.4.1.6876.3.2.3.0";
    private static final String CMD_STR = "esxtop -n 1 -d 2 ";

    private static final String CPU_COUNT_KEY_WORD = "Processor Time";
    private static final String POWER_KEY_WORD = "Power Usage Cap Watts";

    @Override
    public List<String[]> find(String ip, Map params) {
        String[] oids = { SYS_NAME_OID, SYS_DESCRIPT_OID, PHY_CPU_NUM_OID, PHY_MEM_SIZE_OID };
        List<String[]> list = new ArrayList<String[]>();
        String[] osInfo = new String[9];
        osInfo[0] = "";
        osInfo[1] = "EsxOS";
        osInfo[2] = "vspere Esx 主机";
        try {
            Map<Integer, String> values = SnmpUtil.getValueBySnmp(ip, oids, "public");
            osInfo[3] = values.get(0);
            osInfo[4] = values.get(1);
            osInfo[5] = values.get(2);
            osInfo[6] = null;
            if (values.get(3) != null && !values.get(3).equals("")) {
                long memSize = Long.parseLong(values.get(3));
                memSize = memSize / 1024;
                osInfo[7] = memSize + "";
            }

            list.add(osInfo);
        } catch (Exception e) {
            logger.error("error find esx host.", e);
        }
        BufferedReader reader = null;
        String strLine = null;
        boolean flag = true;
        String name = null;
        String value = null;
        try {
            reader = CommandReader.getReader(ip, CMD_STR, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                if (flag) {
                    name = strLine;
                    flag = false;
                } else {
                    value = strLine;
                }
            }
        } catch (Exception e) {
            logger.error("error execute esx host cmd : esxtop ", e);
        }
        int cpu_count = 0;
        if (name != null && value != null) {
            String[] names = name.split("[,]");
            String[] values = value.split("[,]");

            for (int i = 0; i < names.length; i++) {
                if (names[i].indexOf(CPU_COUNT_KEY_WORD) != -1) {
                    cpu_count++;
                }
                if (names[i].indexOf(POWER_KEY_WORD) != -1) {
                    osInfo[8] = values[i];
                    if (osInfo[8] != null) {
                        osInfo[8] = org.apache.commons.lang.StringUtils.remove(osInfo[8], "\"");
                    }
                }
            }
            osInfo[6] = (cpu_count - 1) + "";
        }
        return list;
    }

}
